﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LeadVinaglue.Models;
using LeadVinaglue.Models.ViewPages;
using LeadVinaglue.Utility;
using System.Configuration;

namespace LeadVinaglue.Repository
{
    public class HomeManagementRepository : BaseRepository
    {
        public static string ProductImagesURL = ConfigurationManager.AppSettings["ProductImagesURL"].ToString();
        public static int NumOfProductNotification = int.Parse(ConfigurationManager.AppSettings["NumOfProductNotification"].ToString());
        LeadVinaglueEntities entities = new LeadVinaglueEntities();

        public void GetModel(ref HomeViewModel model)
        {
            // Set value
            model.TopDocument = GetAllProductBy(1, ProductImagesURL, 1, 12);
            model.TopBook = GetAllProductBy(2, ProductImagesURL, 1, 12);
            model.TopGallery = GetAllProductBy(3, ProductImagesURL, 1, 12);
            model.HotInWeek = GetAllProductInWeek(ProductImagesURL, 1, 20);
        }
               

        #region Build
        private List<ProductItem> GetAllProductBy(int typeId, string productImageUrl, int pageIndex, int pageSize)
        {
            List<ProductItem> list = new List<ProductItem>();
            var products = (from c in entities.Products
                            join ca in entities.Categories on c.CategoryID equals ca.ID
                            where c.TypeId == typeId && c.Active == true
                            select new
                                {
                                    ProductId = c.ID,
                                    TypeId = c.TypeId,
                                    Title = c.Title,
                                    ImageUrl = c.ImageURL
                                }).ToList();

            products = products.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            
            // Add list
            foreach (var item in products)
            {
                ProductItem model = new ProductItem();
                model.Title = StringClearFormat.Clear35(item.Title);
                model.ImageUrl = (!string.IsNullOrEmpty(item.ImageUrl)) ? string.Format("{0}/{1}", productImageUrl, item.ImageUrl) : "/images/noimagemedium.jpg";
                model.LinkUrl = string.Format("/xem-tai-lieu/{0}.{1}.html", StringClearFormat.ClearSpecials(item.Title), item.ProductId);

                list.Add(model);
            }

            return list;
        }
        
        private List<ProductItem> GetAllProductInWeek(string productImageUrl, int pageIndex, int pageSize)
        {
            DateTime firstDate = LeadTools.GetFirstDateOfWeek(DateTime.Now.Date, DayOfWeek.Sunday);
            DateTime lastDate = LeadTools.GetLastDateOfWeek(DateTime.Now.Date, DayOfWeek.Saturday);
            lastDate = LeadTools.EndOfDay(lastDate.AddDays(1));

            List<ProductItem> list = new List<ProductItem>();
            var products = (from c in entities.Products
                            join ca in entities.Categories on c.CategoryID equals ca.ID
                            where c.Active == true && c.DateCreated >= firstDate && c.DateCreated <= lastDate
                            orderby c.Reviewed descending
                            select new
                            {
                                ProductId = c.ID,
                                TypeId = c.TypeId,
                                Title = c.Title,
                                ImageUrl = c.ImageURL
                            }).ToList();

            products = products.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            // Add list
            foreach (var item in products)
            {
                ProductItem model = new ProductItem();
                model.Title = StringClearFormat.Clear35(item.Title);
                model.ImageUrl = (!string.IsNullOrEmpty(item.ImageUrl)) ? string.Format("{0}/{1}", productImageUrl, item.ImageUrl) : "/images/noimagemedium.jpg";
                model.LinkUrl = string.Format("/xem-tai-lieu/{0}.{1}.html", StringClearFormat.ClearSpecials(item.Title), item.ProductId);

                list.Add(model);
            }

            return list;
        }
        #endregion
    }
}